#include "groupsessiondetailwidget.h"
#include"model/datacenter.h"

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent)
    :QDialog(parent)
{
    // 设置关闭后⾃动销毁
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 1. 设置基本属性
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(410, 600);
    this->setStyleSheet("QWidget {background-color: rgb(255, 255, 255); }");
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/images/logo.png"));

    // 2. 创建布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(50, 50, 50, 50);
    layout->setSpacing(10);
    this->setLayout(layout);

    // 3. 创建成员列表区
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setFixedSize(310, 350);
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // ⼀定要添加这个设置, 否则⽆法正确显⽰.
    scrollArea->setWidgetResizable(true);

    // 隐藏⽔平滚动条. 把垂直滚动条设置的细⼀些.
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {width: 2px; background-color: rgb(255, 255, 255); }\
                                                    QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0px;}");

    scrollArea->setStyleSheet("QScrollArea { border: none;}");
    layout->addWidget(scrollArea);

    container = new QWidget();
    scrollArea->setWidget(container);

    QGridLayout* gridLayout = new QGridLayout();
    gridLayout->setContentsMargins(0, 0, 0, 0);
    gridLayout->setSpacing(10);
    gridLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(gridLayout);

    // 4. 添加邀请按钮
    AvatarItem* addBtn = new AvatarItem(QIcon(":/images/add.png"), "添加");
    gridLayout->addWidget(addBtn, currentRow, currentCol);
    currentCol++;

    // 5. 添加群聊名称
    QLabel* groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setStyleSheet("QLabel {font-weight: 700;}");
    layout->addWidget(groupNameTag);

    QHBoxLayout* hlayout = new QHBoxLayout();
    groupNameLabel = new QLabel();
    hlayout->addWidget(groupNameLabel);

    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setIcon(QIcon(":/images/modify.png"));
    modifyBtn->setFixedSize(30, 30);
    modifyBtn->setIconSize(QSize(20, 20));
    modifyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    modifyBtn->setStyleSheet("QPushButton { border: none; }QPushButton:pressed { background-color: rgb(235, 235, 235);}" );
    hlayout->addWidget(modifyBtn);
    layout->addLayout(hlayout);

    // 6. 添加退群按钮
    QPushButton* exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedSize(310, 50);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    exitGroupBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90, 90,90); border-radius: 5px; }\ "
                                "QPushButton:pressed { background-color: rgb(235,235, 235); }");
    layout->addWidget(exitGroupBtn, 0, Qt::AlignCenter);

// 7. 添加群聊成员数据
#if TEST_UI
    for (int i = 0; i < 20; i++) {
        AvatarItem* avatarItem = new
            AvatarItem(QIcon(":/images/default_avatar.jpg"), "张三");
        this->addMember(avatarItem);
    }
    groupNameLabel->setText("⼈类吃喝⾏为研究⼩组");
#endif

#if LOAD_DATA_FROM_NETWORK
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getMemberListDone, this, &GroupSessionDetailWidget::initMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
#endif
}

void GroupSessionDetailWidget::addMember(AvatarItem *avatarItem)
{
    QGridLayout* layout = dynamic_cast<QGridLayout*>(this->container->layout());
    // 判定是否要换⾏
    if (currentCol >= MAX_COL) {
        currentCol = 0;
        currentRow++;
    }
    layout->addWidget(avatarItem, currentRow, currentCol);
    currentCol++;
}

void GroupSessionDetailWidget::initMembers(const QString &chatSessionId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    QList<model::UserInfo>* memberList = dataCenter->getMemberList(chatSessionId);
    if (memberList == nullptr) {
        LOG() << "获取成员列表失败! chatSessionId=" << chatSessionId;
        return;
    }
    for (auto it = memberList->begin(); it != memberList->end(); ++it) {
        AvatarItem* avatarItem = new AvatarItem(it->avatar, it->nickname);
        this->addMember(avatarItem);
    }
    // 群聊名称先写成固定值
    groupNameLabel->setText("新建群聊");
}
